package com.sevenstudio.adu.gasing.element;

import java.io.IOException;

import org.andengine.audio.music.Music;
import org.andengine.audio.music.MusicFactory;

import com.sevenstudio.adu.gasing.define.GameEngineConfiguration;
import com.sevenstudio.adu.gasing.engine.GameEngine;
import com.sevenstudio.adu.gasing.game.element.GameElementMusic;

public class AMusic 
{
	GameEngine theEngine;
	Music music;
	public boolean paused;
	
	public AMusic(GameEngine mainActivity, GameElementMusic[] arrayBgm, int index)
	{
		theEngine = mainActivity;
		Load(arrayBgm, index);
		paused = false;
	}
	
	void Load(GameElementMusic[] arrayBgm, int index)
	{
		try {
			music = MusicFactory.createMusicFromAsset(theEngine.getMusicManager(), theEngine, arrayBgm[index].getPath());
			music.setLooping(arrayBgm[index].isLooping());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void Play()
	{
		if(!GameEngineConfiguration.use_music)return;
		paused = false;
		music.play();
	}
	
	public void Pause()
	{
		if(!GameEngineConfiguration.use_music)return;
		paused = true;
		music.pause();
	}
	
	public void Stop()
	{
		if(!GameEngineConfiguration.use_music)return;
		paused = false;
		music.pause();
		music.seekTo(0);
	}
	
	public void Resume()
	{
		if(!GameEngineConfiguration.use_music)return;
		if(paused)
		{
			paused = false;
			music.play();
		}
		else
		{
			System.out.println("cannot resume music...");
		}
	}
	
	public Music theMusic()
	{
		return music;
	}
}
